package cn.tlb.gl.democube;

import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created by 汤林冰 on 2015/12/9 14:16.
 */
public class MyRenderer implements GLSurfaceView.Renderer{

    float[] cubedVertex = {
         0.5f,    0.5f,   0.5f, // 0   第1卦限
         0.5f,    0.5f,  -0.5f, // 1   第2卦限
        -0.5f,    0.5f,  -0.5f, // 2   第3卦限
        -0.5f,    0.5f,   0.5f, // 3   第4卦限
         0.5f,   -0.5f,   0.5f, // 4   第5卦限
         0.5f,   -0.5f,  -0.5f, // 5   第6卦限
        -0.5f,   -0.5f,  -0.5f, // 6   第7卦限
        -0.5f,   -0.5f,   0.5f  // 7   第8卦限
    };

    int[] colors = {
        0xFFFF, 0,      0,      0,//r
        0,      0xFFFF, 0,      0,//g
        0,      0,      0xFFFF, 0,//b

        0xFFFF, 0xFFFF, 0,      0,//rg
        0xFFFF, 0,      0xFFFF, 0,//rb
        0,      0xFFFF, 0xFFFF, 0,//gb

        0x7FFF, 0x7FFF, 0,      0,//rg/2
        0x7FFF, 0,      0x7FFF, 0,//rb/2
    };

    byte[] indices = {
        0, 1, 2,
        0, 2, 3,
        0, 3, 7,
        0, 7, 4,
        0, 4, 5,
        0, 5, 1,
        6, 1, 2,
        6, 2, 3,
        6, 3, 7,
        6, 7, 4,
        6, 4, 5,
        6, 5, 1
    };


    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(0,0,0,0);
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glDepthFunc(GL10.GL_LEQUAL);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glEnable(GL10.GL_DEPTH_TEST);
    }
    FloatBuffer pointerBuffer;
    IntBuffer colorBuffer;
    ByteBuffer indicesBuffer;
    public MyRenderer() {
        pointerBuffer = array2FloatBuffer(cubedVertex);
        colorBuffer = array2IntBuffer(colors);
        indicesBuffer = ByteBuffer.wrap(indices);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        float ratio = (float)width/height;
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    }

    MotionEvent event;
    float touchX;
    float touchY;

    float rotateX;
    float rotateY;

    public void setEvent(MotionEvent event) {
        this.event = event;
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        //清除颜色缓存、深度缓存
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        // 启用定点数组
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //启用颜色数组
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        gl.glMatrixMode(GL10.GL_MODELVIEW);

        // -------------绘制三棱锥----------------
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -2f);
        if(event != null) {
            switch(event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    touchX = event.getX();
                    touchY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    rotateX += (event.getX()-touchX)/20;
                    rotateY += ((event.getY()-touchY))/20;
                    break;
                default:
                    break;
            }
        }
        gl.glRotatef(rotateY, 0.2f, 0, 0);
        gl.glRotatef(rotateX, 0, 0.2f, 0);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, pointerBuffer);
        gl.glColorPointer(4, GL10.GL_FIXED, 0, array2IntBuffer(colors));
        gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, indicesBuffer.remaining(), GL10.GL_UNSIGNED_BYTE, indicesBuffer);

        gl.glFinish();
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    }

    private FloatBuffer array2FloatBuffer(float fs[]){
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fs.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
        floatBuffer.put(fs);
        floatBuffer.position(0);
        return floatBuffer;
    }

    private IntBuffer array2IntBuffer(int ints[]){
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(ints.length*4);
        byteBuffer.order(ByteOrder.nativeOrder());
        IntBuffer intBuffer = byteBuffer.asIntBuffer();
        intBuffer.put(ints);
        intBuffer.position(0);
        return intBuffer;
    }
}
